function Ajax($method, $url, $parmas) {
  return new Promise(function (resolve, reject) {
    let xhr = new XMLHttpRequest();
    let str = "?";
    if ($parmas) {
      for (let key in $parmas) {
        str += `${key}=${$parmas[key]}&`;
      }
    };

    if ($method.toUpperCase() === "GET") {
      xhr.open($method, $url + str.slice(0, -1));
      xhr.send();
    } else {
      xhr.open($method, $url);
      xhr.setRequestHeader("content-type", "application/json");
      xhr.send(JSON.stringify($parmas));
    };

    xhr.addEventListener("readystatechange", function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          resolve(JSON.parse(xhr.response));
        } else {
          reject(JSON.parse(xhr.response));
        }
      }
    });
  });
};//ajax.js


let ul = document.querySelector("ul");
let li_list = document.getElementsByTagName("li");
Ajax("GET", "http://studentback.dfbuff.top/getMethod", {
  type: "图片",
}).then(function (res) {
  console.log(res.result);
  res.result.forEach(function (item, index) {
    console.log(item.content);
    ul.innerHTML += `<li><img src="" alt="1" data-src="${item.content} " class="lazy"></li>`;
  });
});
//懒
loadImg(".lazy");
function loadImg($tag) {
  let img = document.querySelectorAll($tag);
  if (!img.length) return;
  img.forEach(function (el) {
    if (
      document.documentElement.clientHeight +
        document.documentElement.scrollTop >=
      el.offsetTop
    ) {
      el.classList.remove("lazy");
      el.src = el.dataset.src;
      console.log("到达", el);
    }
  });
}
document.addEventListener("scroll", function () {
  loadImg(".lazy");
});
